package com.study.sbsummary.model;

import com.alibaba.fastjson.JSON;
import com.study.sbsummary.config.constant.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;

@AllArgsConstructor
@Builder
public class Response<T> {
    public static final Response SUCCESS = Response.success(null);

    private int code;
    private String status;
    private String reason;
    private String msg;
    private T result;

    public Response() {
        this.code = CodeEnum.GENERAL_SUCCESS.getCode();
        this.status = CodeEnum.GENERAL_SUCCESS.getReason();
    }

    public Response(String status, int code, String reason, String message, T result) {
        this.code = code;
        this.msg = message;
        this.status = status;
        this.reason = reason;
        this.result = result;
    }

    /*
     *	成功返回包，分为 只有对象，对象+消息，对象+reason+msg，code模式
     */
    public static <R> Response<R> success(R result) {
        return success(CodeEnum.GENERAL_SUCCESS, CodeEnum.GENERAL_SUCCESS.getReason(), result);
    }

    public static <R> Response<R> success(CodeEnum errorCode, String message, R result) {
        return success(errorCode, errorCode.getReason(), message, result);
    }

    public static <R> Response<R> success(CodeEnum errorCode, String reason, String message, R result) {
        return new Response<>("success", errorCode.getCode(), reason, message, result);
    }


    /*
     *	失败返回包，分为 只有对象，对象+消息，对象+消息+error，code模式
     */
    public static <R> Response<R> failed(String message, R result) {
        return failed(CodeEnum.GENERAL_FAIL, message, result);
    }

    public static <R> Response<R> failed(CodeEnum errorCode, String message, R result) {
        return failed(errorCode, errorCode.getReason(), message, result);
    }

    public static <R> Response<R> failed(CodeEnum errorCode, String reason, String message, R result) {
        return new Response<>("failed", errorCode.getCode(), reason, message, result);
    }

    public static <R> Response<R> failed(int code, String reason, String message, R result) {
        return new Response<>("failed", code, reason, message, result);
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getReason() {
        return this.reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getMsg() {
        return msg;
    }
}